# 有几种写法
# 第一种，直接导入模块，使用模块名.方法名。
'''
import http.client
http.client.HTTPConnection
'''

# 第二种 导入模块的方法，使用方法名。client.HTTPConnection()
'''
from http import client
client.HTTPConnection()
'''


# 第三种 给导入的模块一个别名，使用别名
from http import client as hc

conn = hc.HTTPConnection("www.baidu.com")
conn.request("GET", "/")
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
print(data.decode())
conn.close()



# 打印响应状态和原因
conn = hc.HTTPConnection("www.baidu.com")
conn.request("GET", "/")
response = conn.getresponse()
print("Status:", response.status)
print("Reason:", response.reason)
print("Response data:", response.read().decode())
conn.close()

#  设置请求头
conn = hc.HTTPConnection("www.example.com")
conn.putrequest("GET", "/")
conn.putheader("User-Agent", "My User Agent")
conn.endheaders()
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
print(data.decode())
conn.close()

# 打印cookies
conn = hc.HTTPConnection("www.example.com")
conn.request("GET", "/")
response = conn.getresponse()
print("Cookies:", response.getheader("Set-Cookie"))
conn.close()
